package com.huxin.servelt.admin;


import com.alibaba.fastjson.JSON;
import com.huxin.pojo.BookHisInfo;
import com.huxin.pojo.BooksInfo;
import com.huxin.pojo.ConditionalQueryDTO;
import com.huxin.pojo.PageBean;
import com.huxin.service.Userservice;
import sun.misc.ConditionLock;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/admin/findAllBookHisInfo")
public class AdminFindAllBookHisInfoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String s = req.getReader().readLine();
        ConditionalQueryDTO conditionalQueryDTO = JSON.parseObject(s, ConditionalQueryDTO.class);
        Userservice userservice = new Userservice();
        PageBean<BookHisInfo> bean=null;

        int page=1;//开始页
        int size=10;//每页显示十条数据
        if(null!=(req.getParameter("page"))){
            page= Integer.parseInt(req.getParameter("page"));
        }
        if(null!=(req.getParameter("size"))){
            size=Integer.parseInt(req.getParameter("size"));
        }
//        book_name
//        name
//        card
        if (conditionalQueryDTO.getType()==null||conditionalQueryDTO.getText()==null){
            bean=userservice.findAll(page,size);
        } else if ("bid".equals(conditionalQueryDTO.getType())) {//按照书籍号查询
            bean=userservice.findAllbyBid(conditionalQueryDTO,page,size);
        } else if ("username".equals(conditionalQueryDTO.getType())) {//按照用户账号查询
            bean=userservice.findAllbyUserName(conditionalQueryDTO,page,size);
        } else if ("book_name".equals(conditionalQueryDTO.getType())) {//按照书籍名查询
            bean=userservice.findAllbyBookName(conditionalQueryDTO,page,size);
        } else if ("return".equals(conditionalQueryDTO.getType())) {//搜索未归还
            bean=userservice.findAllNotReturn(page,size);
        }
        String jsonString = JSON.toJSONString(bean);
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
